﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CodeBackup.Code
{
    internal class Upload
    {


        public async Task<string> upAsync(string FilePath)
        {

            string filePath = FilePath; // 修改为您要上传的文件路径
            string apiUrl = $"{ini.Api}/api/Edi/PubUploaddbFile"; // 修改为您的 API URL

            using (var client = new HttpClient())
            using (var form = new MultipartFormDataContent())
            {
                // 读取文件内容
                var fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read);
                var fileContent = new StreamContent(fileStream);

                // 设置文件内容的类型（可选）
                fileContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/octet-stream");
                form.Add(fileContent, "file", Path.GetFileName(filePath)); // "file" 是 API 中接收的参数名


                try
                {
                    // 上传文件
                    var response = await client.PostAsync(apiUrl, form);
                    response.EnsureSuccessStatusCode();

                    return await response.Content.ReadAsStringAsync();

                }
                catch (Exception ex)
                {
                    Console.WriteLine("Error uploading file: " + ex.Message);
                    return string.Empty;
                }
                finally
                {
                    fileStream.Close(); // 确保流被关闭
                }
            }
        }

        public async Task upAsync(string FilePath,string topc)
        {
            
            string filePath = FilePath; // 修改为您要上传的文件路径
            string apiUrl = $"{ini.Api}/api/Edi/UploadFile?topic={topc}&repcode={ini.repcode}"; // 修改为您的 API URL

            using (var client = new HttpClient())
            using (var form = new MultipartFormDataContent())
            {
                // 读取文件内容
                var fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read);
                var fileContent = new StreamContent(fileStream);

                // 设置文件内容的类型（可选）
                fileContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/octet-stream");
                form.Add(fileContent, "file", Path.GetFileName(filePath)); // "file" 是 API 中接收的参数名


                try
                {
                    // 上传文件
                    var response = await client.PostAsync(apiUrl, form);
                    response.EnsureSuccessStatusCode();

                    var result = await response.Content.ReadAsStringAsync();
                    Console.WriteLine("File uploaded successfully: " + result);
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Error uploading file: " + ex.Message);
                }
                finally
                {
                    fileStream.Close(); // 确保流被关闭
                }
            }
        }
    }
}
